#include "widget.h"
#include <cmath>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
}

Widget::~Widget()
{
}

void Widget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);

    // 绘制红旗背景（红色矩形）
    QRect flagRect(0, 0, width(), height());
    painter.fillRect(flagRect, Qt::red);

    // 计算大五角星的位置和大小
    int largeStarRadius = qMin(width(), height()) / 10;
    QPointF largeStarCenter(width() * 0.2, height() * 0.2);

    // 绘制大五角星
    QPainterPath largeStarPath;
    double largeStarAngle = 0;
    double largeStarStep = 2 * M_PI / 5;
    for (int i = 0; i < 5; ++i) {
        double x = largeStarCenter.x() + largeStarRadius * cos(largeStarAngle);
        double y = largeStarCenter.y() - largeStarRadius * sin(largeStarAngle);
        if (i == 0) {
            largeStarPath.moveTo(x, y);
        } else {
            largeStarPath.lineTo(x, y);
        }
        largeStarAngle += 2 * largeStarStep;
    }
    largeStarPath.closeSubpath();
    painter.setPen(Qt::NoPen);
    painter.setBrush(Qt::yellow);
    painter.drawPath(largeStarPath);

    // 计算小五角星的位置和大小
    int smallStarRadius = largeStarRadius / 3;
    QPointF smallStarCenters[4] = {
        QPointF(largeStarCenter.x() + largeStarRadius * 1.5, largeStarCenter.y() - largeStarRadius * 0.5),
        QPointF(largeStarCenter.x() + largeStarRadius * 1.5, largeStarCenter.y() + largeStarRadius * 0.5),
        QPointF(largeStarCenter.x() + largeStarRadius * 0.5, largeStarCenter.y() + largeStarRadius * 1.5),
        QPointF(largeStarCenter.x() - largeStarRadius * 0.5, largeStarCenter.y() + largeStarRadius * 1.5)
    };

    // 绘制小五角星
    for (int i = 0; i < 4; ++i) {
        QPainterPath smallStarPath;
        double smallStarAngle = 0;
        double smallStarStep = 2 * M_PI / 5;
        for (int j = 0; j < 5; ++j) {
            double x = smallStarCenters[i].x() + smallStarRadius * cos(smallStarAngle);
            double y = smallStarCenters[i].y() - smallStarRadius * sin(smallStarAngle);
            if (j == 0) {
                smallStarPath.moveTo(x, y);
            } else {
                smallStarPath.lineTo(x, y);
            }
            smallStarAngle += 2 * smallStarStep;
        }
        smallStarPath.closeSubpath();
        painter.drawPath(smallStarPath);
    }
}
